<?php
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/local/blackboxsurvey/lib.php');
require_once($CFG->dirroot . "/local/blackboxsurvey/forms.php");
define('AJAX_SCRIPT', true);

//yeu cau login,sau khi login tu dong chuyen sang trang chu
global $USER;
if ($USER->id == null){
    $SESSION->homepage = true;
    require_login($SITE);
}
if($SESSION->homepage){
    $SESSION->homepage = null;
    redirect( $CFG->wwwroot);
}

//object class
require_once($CFG->dirroot . "/local/blackboxsurvey/class/questions.php");
require_once($CFG->dirroot . "/local/blackboxsurvey/class/question_choice.php");

$id = optional_param('id',null, PARAM_TEXT);
$path_image = $CFG->wwwroot .'/local/blackboxsurvey/image/';
/* Viet doan code cua ban o day */

//get question detail
$b_ques = new b_questions();
$question = $b_ques->get_question_detail($id);
$ques_detail['path_image'] = $path_image;
//add question to array to show
$ques_detail['id'] = $question->id;
//$ques_detail['content'] = $question->content;
$ques_detail['content'] = preg_replace(array('#<p>#', '#</p>#'), '', $question->content, 1);
//echo "<script>alert('".$ques_detail['content']."');</script>";

//lay option cua cau hoi neu co
$b_ques_choice = new b_question_choice();
$choice = $b_ques_choice->get_choice_by_question($id);
$ques_detail['num_opt'] = count($choice);

//add option to array to show
$index = 1;
foreach($choice as $r) {
    $ques_detail['opt_ct'.$index] = $r->content;
    $index++;
}
    
$mform = new form_edit_multichoice_question(null, $ques_detail);
$mform->display();
//
echo "<script  type='text/javascript'>check_all_textbox();</script>";
if ($fromform = $mform->get_data()) 
{	
    $question['id'] = $fromform->txtid;
    $question['content'] = $fromform->txtcont;
	
    $update_ques = new b_questions();
    $update_ques->update_question($question);

    //question choice moi
    //$opt_cache = $fromform->opt_cache;
    //$ques_choice = explode('~', $opt_cache);
    $ques_choice = $_POST['arr_opt'];
    
    //xoa cac question choice cu
    $b_ques_choice = new b_question_choice();
    $b_ques_choice->delete_choice_by_questionID($question['id']);
    
    //add question choice moi
    $b_ques_choice->add_choices($question['id'], $ques_choice);
	//close colorbox
	echo "<script>parent.$.fn.colorbox.close();</script>";
    
}



/*------Ket thuc doan code cua ban-------*/

